﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SuperX.Helper.Util
{
    public class EnumUtil
    {
        #region 枚举类型转化成字典

        /// <summary>
        /// 将枚举类型转化为字典，把枚举的类型作为参数传入。参数传的是类型
        /// </summary>
        /// <typeparam name="TEnum">要进行转换的枚举类型</typeparam>
        /// <typeparam name="TKey">字典的Key类型</typeparam>
        /// <typeparam name="Tvalue">字典的Value类型</typeparam>
        /// <returns></returns>
        public static Dictionary<TKey, Tvalue> ConvertEnumToDictionary<TEnum, TKey, Tvalue>(Dictionary<TKey, Tvalue> dic = null) where TEnum : struct
        {
            if (dic == null) dic = new Dictionary<TKey, Tvalue>();

            var type = typeof(TEnum);
            if (!type.IsEnum) return dic;
            var list = Enum.GetValues(type);
            foreach (TEnum enumItem in Enum.GetValues(type))
            {
                dic.Add((TKey)Convert.ChangeType(enumItem, typeof(TKey)), (Tvalue)Convert.ChangeType(enumItem, typeof(Tvalue)));
            }
            return dic;
        }

        #endregion 枚举类型转化成字典
    }
}